package com.googlecode.imanager.client;

import com.googlecode.imanager.client.bridge.BridgeManager;
import com.googlecode.imanager.client.bridge.IApplication;
import com.googlecode.imanager.client.bridge.IControl;
import com.googlecode.imanager.client.event.EventUtils;
import com.googlecode.imanager.client.event.KeyEvent;
import com.googlecode.imanager.client.event.MouseEvent;
import com.googlecode.imanager.client.event.PhaseEvent;
import com.googlecode.imanager.client.form.FocusControl;
import com.googlecode.imanager.client.form.FormUtils;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class Manager extends BridgeManager {
    
    //
    // Read-only access to implementation's properties
    //

    public static IControl lookupImpl(Control control){
        return control.lookupBridge();
    }        

    public static IApplication lookupImpl(Application application){
        return application.getBridge();
    }        

    public static boolean hasImpl(Control control) {
        return control.bridge != null;
    }
    
    public static boolean hasImpl(Application application) {
        return application.bridge != null;
    }
    
    public static IControl getImpl(Control control) {
        return control.bridge;
    }
    
    public static IApplication getImpl(Application application) {
        return application.bridge;
    }
        
    public static Application getApplication(Document document) {
        return document.application;
    }
            
    //
    // Implementation modifications
    //
    
    public void setImpl(Control control, IControl impl) {
        control.bridge = impl;
    }
    
    public void setImpl(Application application, IApplication impl) {
        application.bridge = impl;
    }
        
    public void setInsets(Container container, 
            int insetLeft, int insetTop, int insetRight, int insetBottom){
        container.setInsets(insetLeft, insetTop, insetRight, insetBottom);
    }
    
    //
    // Firing events
    //

    public void fireParentResized(Control source) {
        source.onParentResized(source.getParentSize());
    }

    public void fireClick(Control target) {
        target.onClick();
    }

    public void firePhaseEvent(PhaseEvent e) {
        EventUtils.setSimulated(e, false);
        e.getTarget().getApplication().onPhaseEvent(e);
    }

    public void fireMouseEvent(MouseEvent e) {
        EventUtils.setSimulated(e, false);
        e.getTarget().onMouseEvent(e);
    }
    
    public void fireKeyEvent(KeyEvent e) {
        EventUtils.setSimulated(e, false);
        e.getTarget().onKeyEvent(e);
    }

    public void fireBlur(Focusable source) {
        Application app = ((Control)source).getApplication();
        if (app.focusControl == source){
            app.focusControl = null;
            doFireFocus(source, false);
        }
    }
    
    public void fireFocus(Focusable source) {
        Application app = ((Control)source).getApplication();
        if (app.focusControl != source){
            if (app.focusControl != null){
                fireBlur((FocusControl) app.focusControl);
            }
            if (app.focusControl == null){
                app.focusControl = source;
                doFireFocus(source, true);
            }
        }
    }

    private void doFireFocus(Focusable source, boolean focus){
        if (source instanceof FocusControl){
            FormUtils.fireFocus((FocusControl) source, focus);
        }
    }
    
}
